import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common import keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

def time_c():
    return time.strftime('%Y%m%d%H%M',time.localtime())

class Base:
    def __init__(self,driver):
        self.driver=driver
    # 浏览器操作
    def max_web(self):
        self.driver.maximize_window()
    def min_web(self):
        self.driver.minimize_window()
    def re_web(self):
        self.driver.refresh()
    def back_web(self):
        self.driver.back()
    def open_url(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.1).until(lambda driver : driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.save_scr('element')
    def text(self,loc):
        return self.find_element(loc).text
    # 截图
    def save_scr(self,name):
        self.driver.save_screenshot(f'./error/{name}{time_c()}.png')
    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 切换页面
    def qh(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 进入frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 退出frame框架
    def quit_frame(self):
        self.driver.switch_to.default_content()
    # 键盘操作
    def ENTER(self,loc):
        self.send_keys(loc,keys.Keys.ENTER)
    def CTRL(self,loc):
        self.send_keys(loc,keys.Keys.CONTROL)
    def ctrl_c(self,loc):
        self.send_keys(loc,(keys.Keys.CONTROL,'c'))
    def ctrl_a(self,loc):
        self.send_keys(loc,(keys.Keys.CONTROL,'a'))
    def ctrl_x(self,loc):
        self.send_keys(loc,(keys.Keys.CONTROL,'x'))
    def ctrl_v(self,loc):
        self.send_keys(loc,(keys.Keys.CONTROL,'v'))
    def shift(self,loc):
        self.send_keys(loc,keys.Keys.SHIFT)
    # 鼠标操作
    def sb(self):
        return ActionChains(self.driver)
    def double_click(self,loc):
        self.sb().double_click(self.find_element(loc)).perform()
    def drag_and_dorp(self,x,y):
        self.sb().drag_and_drop(self.find_element(x),self.find_element(y)).perform()
    def context_click(self):
        self.sb().context_click().perform()
    def move_to_element(self,loc):
        self.sb().move_to_element(self.find_element(loc)).perform()
    def move_to_element_with_offset(self,loc,x,y):
        self.sb().move_to_element_with_offset(self.find_element(loc),x,y).perform()
    def click_and_hold(self,loc):
        self.sb().click_and_hold(self.find_element(loc)).perform()
    # 下拉列表处理
    def select(self,loc):
        return Select(self.find_element(loc))
    def select_index(self,loc,index):
        self.select(loc).select_by_index(index)
    def select_text(self,loc,text):
        self.select(loc).select_by_visible_text(text)
    def select_value(self,loc,value):
        self.select(loc).select_by_value(value)
    # 弹框处理
    def alter(self):
        return self.driver.switch_to.alert
    def alter_accept(self):
        self.alter().accept()
    def alter_dismiss(self):
        self.alter().dismiss()
    def alter_send(self,info):
        self.alter().send_keys(info)
    # 滚动
    def scroll(self,x):
        self.driver.execute_script(f'ScrollTo(0,{x})')
    def scroll_end(self):
        self.driver.execute_script('ScrollTo(0,decunt.body.scrollHeight)')